<?php
$objUserForm = new BM_PHPForm();
$objUserForm->UseLocator();
$objUserForm->SetIndent("          ");

if (isset($_REQUEST['msg']) && $_REQUEST['msg'] == "add"){
	$objUserForm->SetInfor("useradd" , "User Added Successfully !");
}

$entUser  = UserController::find($id);
$entUser  = UserController::findUserRoles($entUser);
$arrRoles = RoleController::findAll();

if ($objUserForm->IsPost()){
	$objUserForm->ChEmptiness("name", "Name is empty.");
	$objUserForm->ChMaxLength("name", "Name must not be more than 40 chars.", 40);
	$objUserForm->ChWithRegex("name", "Name Have Invalid Chars.", "/^([a-zA-Z0-9_\\.\\-\s])*$/");
		
	if ($objUserForm->IsValid()){
		$entUser = new User($entUser->username,"",$objUserForm->arrPostData['name']);
		UserController::update($entUser);
		UserController::deleteRoles($entUser->username);
		$uroles = array();
		foreach($arrRoles as $role){
			if(isset($objUserForm->arrPostData[$role->id])){
				$uroles[]=$role->id;
			}
		}
		if (!sizeof($uroles) == 0){
			UserController::saveRoles($entUser,$uroles);
		}
		$objUserForm->SetInfor("submit","User Updated Successfully !");
	}	
}

print $objUserForm->ErrMsgs();
print $objUserForm->InfMsgs();

print $objUserForm->Open("userform","User Update Form");
print $objUserForm->HiddenField("action","$action");
print $objUserForm->HiddenField("id" 	,"$id");
print $objUserForm->TextBox("username" 	,"Username:","$entUser->username",array('readonly'=>'readonly'));
print $objUserForm->TextBox("name" 		,"Name_:"	,"$entUser->name");
print "		<div style=\"padding-left: 78px;padding-bottom: 10px;\">Password:  <a href=\"../$www/index_user.php?action=updatep&id=$entUser->username\">Change Password</a></div>\r\n";
print "		<div style=\"padding-left: 102px;\">Roles:</div>";
foreach ($arrRoles as $role){
	if (in_array($role->id , $entUser->roles)){
		print $objUserForm->CheckBox("$role->id",$role->name,array(),true,$role->id);
	}else{
		print $objUserForm->CheckBox("$role->id",$role->name,array(),false,$role->id);
	}
}
print $objUserForm->SubmitButton("submit", "Update", array(), "submit");
print $objUserForm->Close();
?>